﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Biz;
using Cn.Edwon.Spring;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Client.ControlHelper;
using Cn.Cellula.Module.Stat.Model;
using Cn.Cellula.Client;
using Cn.Edwon.Web;

namespace Cn.Cellula.Website.Admin.Module.Stat {
    public partial class List : PageSupport {
        IBiz biz = BizFactory.Get<IBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            GvHelper.AppendEventHandler(ref this.gvList);
            if (!Page.IsPostBack) {
                LoadEOList();
            }

        }
        #region 对 EO 操作
        private void LoadEOList() {
            IList<ModVStatEO> list = biz.LoadAll<ModVStatEO>();
            this.gvList.DataSource = list;
            this.gvList.Attributes[GvHelper.ATTR_TOTAL_COUNT] = (list != null) ? list.Count.ToString() : "0";
            this.gvList.DataBind();
            if (list != null && list.Count > 0) {
                String eoKey = Request.QueryString["sid"];
                if (!String.IsNullOrEmpty(this.hid_EOKey.Value)) {
                    eoKey = this.hid_EOKey.Value;
                } else if (!String.IsNullOrEmpty(Request.QueryString["sid"])) {
                    eoKey = Request.QueryString["sid"];
                } else {
                    eoKey = list[0].StatKey;
                }
                GvHelper.GvSelected(ref this.gvList, eoKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
                this.hid_EOKey.Value = eoKey;
                this.LoadSeies(eoKey);
                this.LoadFilters(eoKey);
                this.LoadColumns(eoKey);
            }
        }
        protected void btn_EOAdd_Click(object sender, EventArgs e) {
            Response.Redirect("EditStat.aspx");
        }

        protected void btn_EOUpdate_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count == 1) {
                Response.Redirect("EditStat.aspx?id=" + keys[0]);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoSingleSelect);
                return;
            }
        }

        protected void btn_EODel_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                foreach (String k in keys) {
                    biz.Delete<ModTStatEO>(k);
                }
                this.LoadEOList();
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
        }
        protected void gvPager_OnSelectedIndexChanged(object sender, EventArgs e) {
            DropDownList ddl = (DropDownList)sender;
            this.gvList.PageIndex = Convert.ToInt32(ddl.SelectedValue) - 1;
            this.hid_EOKey.Value = "";
            LoadEOList();
        }
        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            this.gvList.PageIndex = e.NewPageIndex;
            this.hid_EOKey.Value = "";
            LoadEOList();
        }
        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            if (e.CommandName == "CheckSubList") {
                String eoKey = e.CommandArgument.ToString();
                this.hid_EOKey.Value = eoKey;
                this.LoadEOList();
            }
        }
        #endregion

        #region 对Series操作
        private void LoadSeies(String rStatKey) {
            IList<ModVStatSeriesEO> list = biz.Find<ModVStatSeriesEO>("from ModVStatSeriesEO eo where eo.StatKey='" + rStatKey + "' order by eo.OrderNumber desc");
            this.gvSubList.DataSource = list;
            this.gvSubList.DataBind();
        }

        protected void btn_AddSub_Click(object sender, EventArgs e) {
            Response.Redirect("EditSeries.aspx?sid=" + this.hid_EOKey.Value);
        }

        protected void btn_UpdateSub_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvSubList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count == 1) {
                Response.Redirect("EditSeries.aspx?sid=" + this.hid_EOKey.Value + "&id=" + keys[0]);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoSingleSelect);
                return;
            }
        }

        protected void btn_DelSub_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvSubList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                foreach (String k in keys) {
                    biz.Delete<ModTStatSeriesEO>(k);
                }
                this.LoadEOList();
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
        }
        #endregion

        #region 对Params操作
        private void LoadFilters(String rParentKey) {
            this.gvFilter.DataSource = biz.Find<ModVStatParamEO>("from ModVStatParamEO eo where eo.StatKey='" + rParentKey + "' order by eo.OrderNumber desc ");
            this.gvFilter.DataBind();
        }

        protected void btn_Add3_Click(object sender, EventArgs e) {
            Response.Redirect("EditParam.aspx?sid=" + this.hid_EOKey.Value);
        }

        protected void btn_Update3_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvFilter, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count == 1) {
                Response.Redirect("EditParam.aspx?sid=" + this.hid_EOKey.Value + "&id=" + keys[0]);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoSingleSelect);
                return;
            }
        }

        protected void btn_Del3_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvFilter, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                foreach (String k in keys) {
                    biz.Delete<ModTStatParamEO>(k);
                }
                this.LoadEOList();
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
        }
        #endregion

        #region 对Column操作
        private void LoadColumns(String rStatKey) {
            IList<ModTStatColumnsEO> list = biz.Find<ModTStatColumnsEO>("from ModTStatColumnsEO eo where eo.StatKey='" + rStatKey + "' order by eo.OrderNumber desc");
            this.gvColumns.DataSource = list;
            this.gvColumns.DataBind();
        }

        protected void btn_AddColumn_Click(object sender, EventArgs e) {
            Response.Redirect("EditColumn.aspx?sid=" + this.hid_EOKey.Value);
        }

        protected void btn_UpdateColumn_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvColumns, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count == 1) {
                Response.Redirect("EditColumn.aspx?sid=" + this.hid_EOKey.Value + "&id=" + keys[0]);
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoSingleSelect);
                return;
            }
        }

        protected void btn_DelColumn_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvColumns, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys != null && keys.Count > 0) {
                foreach (String k in keys) {
                    biz.Delete<ModTStatSeriesEO>(k);
                }
                this.LoadEOList();
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
        }
        #endregion
    }
}